; RUN: opt < %s -load /home/user/Research/llvm-2.9-build-eclipse/lib/LLVMmpia.so \
; RUN:    -arraytracking -S -disable-output

; ModuleID = 'trap.ll'
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
target triple = "i386-pc-linux-gnu"

%struct.MPI_Status = type { i32, i32, i32, i32, i32 }
%struct.ompi_communicator_t = type opaque
%struct.ompi_datatype_t = type opaque
%struct.ompi_predefined_communicator_t = type opaque
%struct.ompi_predefined_datatype_t = type opaque

@ompi_mpi_comm_world = external global %struct.ompi_predefined_communicator_t
@ompi_mpi_float = external global %struct.ompi_predefined_datatype_t
@.str = private constant [38 x i8] c"With n = %d trapezoids, our estimate\0A\00", align 4
@.str1 = private constant [36 x i8] c"of the integral from %f to %f = %f\0A\00", align 4

define i32 @main(i32 %argc, i8** %argv) nounwind {
entry:
  %argc_addr = alloca i32, align 4
  %argv_addr = alloca i8**, align 4
  %my_rank = alloca i32
  %p = alloca i32
  %integral = alloca float
  %status = alloca %struct.MPI_Status
  %"alloca point" = bitcast i32 0 to i32
  store i32 %argc, i32* %argc_addr
  store i8** %argv, i8*** %argv_addr
  %0 = call i32 @MPI_Init(i32* %argc_addr, i8*** %argv_addr) nounwind
  %1 = call i32 @MPI_Comm_rank(%struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), i32* %my_rank) nounwind
  %2 = call i32 @MPI_Comm_size(%struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), i32* %p) nounwind
  %3 = fsub float 1.000000e+00, 0.000000e+00
  %4 = sitofp i32 1024 to float
  %5 = fdiv float %3, %4
  %6 = load i32* %p, align 4
  %7 = sdiv i32 1024, %6
  %8 = load i32* %my_rank, align 4
  %9 = mul nsw i32 %8, %7
  %10 = sitofp i32 %9 to float
  %11 = fmul float %10, %5
  %12 = fadd float %11, 0.000000e+00
  %13 = sitofp i32 %7 to float
  %14 = fmul float %13, %5
  %15 = fadd float %14, %12
  %16 = call float @Trap(float %12, float %15, i32 %7, float %5) nounwind
  store float %16, float* %integral, align 4
  %17 = load i32* %my_rank, align 4
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %bb, label %bb5

bb:                                               ; preds = %entry
  %19 = load float* %integral, align 4
  br label %bb3

bb1:                                              ; preds = %bb3
  %integral2 = bitcast float* %integral to i8*
  %20 = call i32 @MPI_Recv(i8* %integral2, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_float to %struct.ompi_datatype_t*), i32 %source.0, i32 0, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), %struct.MPI_Status* %status) nounwind
  %21 = load float* %integral, align 4
  %22 = fadd float %total.1, %21
  %23 = add nsw i32 %source.0, 1
  br label %bb3

bb3:                                              ; preds = %bb1, %bb
  %total.1 = phi float [ %19, %bb ], [ %22, %bb1 ]
  %source.0 = phi i32 [ 1, %bb ], [ %23, %bb1 ]
  %24 = load i32* %p, align 4
  %25 = icmp slt i32 %source.0, %24
  br i1 %25, label %bb1, label %bb4

bb4:                                              ; preds = %bb3
  br label %bb7

bb5:                                              ; preds = %entry
  %integral6 = bitcast float* %integral to i8*
  %26 = call i32 @MPI_Send(i8* %integral6, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_float to %struct.ompi_datatype_t*), i32 0, i32 0, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*)) nounwind
  br label %bb7

bb7:                                              ; preds = %bb5, %bb4
  %total.0 = phi float [ %total.1, %bb4 ], [ undef, %bb5 ]
  %27 = load i32* %my_rank, align 4
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %bb8, label %bb9

bb8:                                              ; preds = %bb7
  %29 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([38 x i8]* @.str, i32 0, i32 0), i32 1024) nounwind
  %30 = fpext float %total.0 to double
  %31 = fpext float 1.000000e+00 to double
  %32 = fpext float 0.000000e+00 to double
  %33 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([36 x i8]* @.str1, i32 0, i32 0), double %32, double %31, double %30) nounwind
  br label %bb9

bb9:                                              ; preds = %bb8, %bb7
  %34 = call i32 @MPI_Finalize() nounwind
  br label %return

return:                                           ; preds = %bb9
  ret i32 undef
}

declare i32 @MPI_Init(i32*, i8***)

declare i32 @MPI_Comm_rank(%struct.ompi_communicator_t*, i32*)

declare i32 @MPI_Comm_size(%struct.ompi_communicator_t*, i32*)

declare i32 @MPI_Recv(i8*, i32, %struct.ompi_datatype_t*, i32, i32, %struct.ompi_communicator_t*, %struct.MPI_Status*)

declare i32 @MPI_Send(i8*, i32, %struct.ompi_datatype_t*, i32, i32, %struct.ompi_communicator_t*)

declare i32 @printf(i8* noalias, ...) nounwind

declare i32 @MPI_Finalize()

define float @Trap(float %local_a, float %local_b, i32 %local_n, float %h) nounwind {
entry:
  %"alloca point" = bitcast i32 0 to i32
  %0 = call float @f(float %local_a) nounwind
  %1 = call float @f(float %local_b) nounwind
  %2 = fadd float %0, %1
  %3 = fdiv float %2, 2.000000e+00
  br label %bb1

bb:                                               ; preds = %bb1
  %4 = fadd float %x.0, %h
  %5 = call float @f(float %4) nounwind
  %6 = fadd float %5, %integral.0
  %7 = add nsw i32 %i.0, 1
  br label %bb1

bb1:                                              ; preds = %bb, %entry
  %integral.0 = phi float [ %3, %entry ], [ %6, %bb ]
  %x.0 = phi float [ %local_a, %entry ], [ %4, %bb ]
  %i.0 = phi i32 [ 1, %entry ], [ %7, %bb ]
  %8 = sub nsw i32 %local_n, 1
  %9 = icmp sge i32 %8, %i.0
  br i1 %9, label %bb, label %bb2

bb2:                                              ; preds = %bb1
  %10 = fmul float %integral.0, %h
  br label %return

return:                                           ; preds = %bb2
  ret float %10
}

define float @f(float %x) nounwind {
entry:
  %"alloca point" = bitcast i32 0 to i32
  %0 = fmul float %x, %x
  br label %return

return:                                           ; preds = %entry
  ret float %0
}
